﻿using DevExpress.Utils;
using Lamex.Base;
using Lamex.SystemManager.BLL;
using Lamex.Common;
using Lamex.Library;
using Lamex.Library.CommonClass;
using Lamex.SystemManager.Model;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lamex.Interface;
using DevExpress.XtraEditors;

namespace Lamex.SystemManager.UI
{
    public partial class frmUser : frmBaseDataForm
    {
        public frmUser()
        {
            InitializeComponent();
            InitializeForm();
            this.Load += new EventHandler(frmUser_Load);
        }
        public string IsEdit = "";
        public string KeysName = "";
        protected override void InitializeForm()
        {
            _SummaryView = new DevGridView(gridView2);
            gridView2.DoubleClick += new EventHandler(OnGridViewDoubleClick); //主表DoubleClict
            frmGridViewCust.RegisterGrid(gridView2);
            DevStyle.SetGridControlLayout(gcView, false);//表格设置   

            DevStyle.SetSummaryGridViewLayout(gridView2);
            DevStyle.SetDetailGridViewLayout(gridView2);
            ShowSummary(); //下载显示数据.
            tpSummary = xtraTabPage1;
            tcBusiness = xtraTabControl1;
            tpSummary.PageVisible = true;
            //tpDetail.PageVisible = false;
            tcBusiness.ShowTabHeader = DefaultBoolean.True;
            tcBusiness.SelectedTabPage = tpSummary;
            txtAccount.CaptionWidth= 60;
            txtUserName.CaptionWidth = 60;
            txtAddrEdit.CaptionWidth = 60;
            txtStartDate.CaptionWidth = 60;
            txtEndDate.CaptionWidth = 60;
            //ShowSummaryPage(true); //一切初始化完毕後显示SummaryPage        
        }
        private void ShowSummary()
        {
            try
            {
                //
                //ucLookUpEdit1.ValueMember = "UserName";
                //ucLookUpEdit1.DisPlayMember = "UserName";
                //ucLookUpEdit1.DataSource = dt;
                ////ucLookUpEdit1.UcPopulateColumns();

                ////ucLookUpEdit1.Item[1].Caption = "用户名称";
                //////ucLookUpEdit1.ForceInitialize();
                ////ucLookUpEdit1.UcItemIndex = 0;
                //DoBindingSummaryGrid(dt); //绑定主表的Grid
                //ShowSummaryPage(true); //显示Summary页面. 
                xtraTabPage2.PageEnabled = false;
                xtraTabPage1.PageEnabled = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        
       

        private void frmUser_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            txtAccount.CaptionVisible = true;
            txtUserName.CaptionVisible = true;
            txtAddrEdit.CaptionVisible = true;
            txtEndDate.CaptionVisible = true;
            txtStartDate.CaptionVisible = true;
        }

        private void ucTextEdit5_UcDoubleClick(object sender, EventArgs e)
        {
            //MessageBox.Show("双击触发成功！");
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        public override void DoView(IButtonInfo sender)
        {
            decimal stri = txtUserName.EditMoney;
            string sqlWhere = "";
            string sql = " select * from Sys_MyUser where 1=1 ";
            if (!string.IsNullOrEmpty(txtAccount.Text))
            {
                sqlWhere += " and Account='" + txtAccount.Text + "'";
            }
            if (!string.IsNullOrEmpty(txtUserName.Text))
            {
                sqlWhere += " and UserName like '%" + txtUserName.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtAddrEdit.Text))
            {
                sqlWhere += " and Address like '%" + txtAddrEdit.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtStartDate.Text))
            {
                sqlWhere += " and CreateTime > '" + txtStartDate.Text + "'";
            }
            if (!string.IsNullOrEmpty(txtEndDate.Text))
            {
                sqlWhere += " and CreateTime < '" + txtEndDate.Text + "'";
            }
            sql = sql + sqlWhere;
            DataTable dt = BaseFactory<TUser>.Instance.GetDataTableBySql(sql);
            DoBindingSummaryGrid(dt);
            base.DoView(sender);
           
        }
        public override void DoAdd(IButtonInfo sender)
        {
            xtraTabPage1.PageEnabled = false;
            xtraTabPage2.PageEnabled = true;
            xtraTabPage2.PageVisible = true;
            txtEditCreateTime.Text = DateTime.Now.ToString();
            IsEdit = "新增";
            xtraTabPage2.Tag = "新增";
            txtEditLoginCenter.Text = "1";
            txtEditDataSet.Text = "1";
            txtIsFirstLogin.Text = "Y";
            base.DoAdd(sender);
        }
        public override void DoEdit(IButtonInfo sender)
        {
            if (gridView2.SelectedRowsCount == 0)
            {
                MessageBox.Show("【没有选中要删除要修改的数据！】");
                return;
            }

            
            int m = gridView2.FocusedRowHandle;
            DataRow dr = gridView2.GetDataRow(m);
            KeysName = dr["isid"].ToString();
            xtraTabPage1.PageEnabled = false;
            xtraTabPage2.PageEnabled = true;
            xtraTabPage2.PageVisible = true;

            IsEdit = "编辑";
            xtraTabPage2.Tag = "编辑";

            //***********************加载数据****************************//

            txtEditAccout.Text = dr["Account"].ToString() ;
            txtEidtAddr.Text = dr["Address"].ToString();
            txtEditPassWord.Text = dr["Password"].ToString();
            txtEditTel.Text = dr["Tel"].ToString() ;
            txtEditEmail.Text = dr["Email"].ToString();
            txtLastLoginTime.Text = dr["LastLoginTime"].ToString();
            txtEditLocked.Text = dr["IsLocked"].ToString();
            txtEditAdmin.Text = dr["FlagAdmin"].ToString();
            txtEditDataSet.Text = dr["DataSets"].ToString();
            txtEditCreateTime.Text = dr["CreateTime"].ToString();
            txtEditCreateTime.Text = DateTime.Now.ToString();
            txtEditUserName.Text = dr["UserName"].ToString();
            txtEditLoginCenter.Text = dr["LoginCounter"].ToString();
            txtIsFirstLogin.Text= dr["IsFirstLogin"].ToString();
            txtIPAddress.Text = dr["IPAddress"].ToString();
            txtMacAddress.Text = dr["MacAddress"].ToString();

            base.DoEdit(sender);
        }
        public override void DoSave(IButtonInfo sender)
        {
            if (txtEditAccout.Alter()) return;
            if (txtIPAddress.Text.Split('.').Count() != 4)
            {
                XtraMessageBox.Show("IP地址格式有问题！");
                return;
            }
            if (txtMacAddress.Text.Split('-').Count() != 6)
            {
                XtraMessageBox.Show("MAC地址格式有问题！");
                return;
            }
            if (IsEdit=="新增")
            {
                TUserInfo model = new TUserInfo();
                model.Account = txtEditAccout.Text;
                model.Address = txtEidtAddr.Text;
                model.Password = txtEditPassWord.Text;
                model.Tel = txtEditTel.Text;
                model.Email = txtEditEmail.Text;
                model.LastLoginTime = DateTime.Now;
                model.IsLocked = txtEditLocked.Text;
                model.FlagAdmin = txtEditAdmin.Text;
                model.DataSets = txtEditDataSet.Text;
                model.CreateTime = Convert.ToDateTime(txtEditCreateTime.Text);
                model.DomainName = txtWinDomain.Text;
                model.LastLoginTime = DateTime.Now;
                model.LoginCounter = int.Parse(txtEditLoginCenter.Text);
                model.UserName = txtEditUserName.Text;
                model.Company_ID = ucCompany1.Value;
                model.CompanyName = ucCompany1.Text;
                model.Dept_ID = ucDept1.Value;
                model.DeptName = ucDept1.Text;
                model.IsFirstLogin = "Y";
                model.IPAddress = txtIPAddress.Text;
                model.MacAddress = txtMacAddress.Text;
                if (BaseFactory<TUser>.Instance.Insert(model))
                {
                    MessageBox.Show("保存存成功！");
                    
                }
                else
                {
                    MessageBox.Show("保存存失败！");
                }
               
            }
            if (IsEdit == "编辑")
            {
                TUserInfo model = new TUserInfo();
                model.isid =int.Parse( KeysName);
                model.Account = txtEditAccout.Text;
                model.Address = txtEidtAddr.Text;
                model.Password = txtEditPassWord.Text;
                model.Tel = txtEditTel.Text;
                model.Email = txtEditEmail.Text;
                model.LastLoginTime = DateTime.Now;
                model.IsLocked = txtEditLocked.Text;
                model.FlagAdmin = txtEditAdmin.Text;
                model.DataSets = txtEditDataSet.Text;
                model.CreateTime = Convert.ToDateTime(txtEditCreateTime.Text);
                model.DomainName = txtWinDomain.Text;
                model.LastLoginTime = DateTime.Now;
                model.LoginCounter = int.Parse(txtEditLoginCenter.Text);
                model.UserName = txtEditUserName.Text;
                model.Company_ID = ucCompany1.Value;
                model.CompanyName = ucCompany1.Text;
                model.Dept_ID = ucDept1.Value;
                model.DeptName = ucDept1.Text;
                model.IPAddress = txtIPAddress.Text;
                model.MacAddress = txtMacAddress.Text;
                model.IsFirstLogin = txtIsFirstLogin.Text;
                if (BaseFactory<TUser>.Instance.Update(model))
                {
                    MessageBox.Show("保存存成功！");

                }
                else
                {
                    MessageBox.Show("保存存失败！");
                }
            }
           
            xtraTabPage2.PageEnabled = false;
            xtraTabPage1.PageEnabled = true;
            DoView(null);
            base.DoSave(sender);
        }
      
        public override void DoDelete(IButtonInfo sender)
        {
            if (gridView2.SelectedRowsCount==0)
            {
                MessageBox.Show("【没有选中要删除的数据，请选择好数据！】");
                return;
            }
            int m = gridView2.FocusedRowHandle;
            DataRow dr = gridView2.GetDataRow(m);
            if (dr != null)
            {
                if (BaseFactory<TUser>.Instance.Delete(dr["isid"].ToString()))
                {
                    MessageBox.Show("【数据删除成功！】");
                    
                }
                else
                {
                    MessageBox.Show("【数据删除失败！】");
                   
                }
            }
            DoView(null);
            base.DoDelete(sender);
        }

        public override void DoCancel(IButtonInfo sender)
        {
            xtraTabPage1.PageEnabled = true;
            xtraTabPage2.PageEnabled = false;
            IsEdit = "";
            base.DoCancel(sender);
        }

       



       
        
    }
}
